<?php

namespace Admin\Controller;

use Think\Controller;

class IndusvalueController extends Controller {

    function __construct() {
        //继承父类
        parent::__construct();

        //判断登录状态
        if (!D('Admin')->islogin()) {//未登录
            $this->error('您尚未登录，请先登录！', U('login/login'), 3);
        }
        //判断操作是否授权
        if (!D('Admin')->isoperate(CONTROLLER_NAME, ACTION_NAME)) {//未授权
            $this->error('您尚无此操作权限！', U('index/welcome'), 3);
        }
    }

    //空方法，防止报错
    public function _empty() {
        $this->index();
    }

    //总行业信息
    public function index() {
        $where="1 and indus_pid=0 ";
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry
                  ->where($where)
                  ->order('indus_id desc')
                  ->limit($Page->firstRow.','.$Page->listRows)
                  ->select();

        foreach($listinfo as $k=>$v){
                $child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
                $listinfo[$k]['childnum']=sizeof($child);
            }          
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        $this->display(); 
    }

    //子行业信息
    public function childindex() {
        $indus_pid=I('get.indus_pid');
        $where="1 and indus_pid=".$indus_pid;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and indus_title like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }

        $industry=M('Industry');
        // 查询满足要求的总记录数
        $count      = $industry->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $industry
                  ->where($where)
                  ->order('indus_id desc')
                  ->limit($Page->firstRow.','.$Page->listRows)
                  ->select();
        $this->assign('indus_pid',$indus_pid);          
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        $this->display(); 
    }
  

    //顶级名称列表
    public function indextop(){
        $indus_id=I('get.indus_id');
        session('indus_id',$indus_id);
        $where="1 and indus_id=".$indus_id;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and top_name like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }
        $topcate=M('topcate');
        // 查询满足要求的总记录数
        $count      = $topcate->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $topcate->where($where)->order('indus_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);
        $this->assign('indus_id',$indus_id);
        $this->display(); 
    }

  //添加顶级评价
    public function addtop(){
        $indus_id=I('get.indus_id');
        if (IS_POST){
            $info=I("info");
            $info['indus_id']=$indus_id;
            $flag=M("Topcate")->add($info);
            if($flag){
                $this->success('添加评级分类成功！',U('indusvalue/indextop',array('indus_id'=>$indus_id)),3);
            }else{
                $this->error('添加评级分类失败！',U('indusvalue/indextop',array('indus_id'=>$indus_id)),3);
            }
        }else{
            $this->assign('indus_id',$indus_id);
            $this->display();
        }
    }

    //修改顶级评价
    public function edittop(){
        $indus_id=I('get.indus_id');
        $top_id=I('get.top_id');
        if (IS_POST){
            $info=I("info");
            $flag=M("Topcate")->where(array('top_id'=>$top_id))->save($info);
            if($flag){
                $this->success('修改顶级名称成功！',U('indusvalue/indextop',array('indus_id'=>$indus_id)),3);
            }else{
                $this->error('修改顶级名称失败！',U('indusvalue/indextop',array('indus_id'=>$indus_id)),3);
            }
        }else{
            $topinfo=M('Topcate')->where(array('top_id'=>$top_id))->find();
            $this->assign('topinfo',$topinfo);
            $this->assign('indus_id',$indus_id);
            $this->display();
        }
    }

    //删除行业
    public function deltop(){
        $top_id=I("get.top_id");
        $indus_id=I('get.indus_id');
        M('Topcate')->where('top_id='.$top_id)->delete();
        $this->success('删除顶级名称成功！',U('indusvalue/indextop',array('indus_id'=>$indus_id)),3);
    }

    //二级名称列表
    public function indexsub(){
        $top_id=I('get.top_id');
        $indus_id=session('indus_id','');
        session('top_id',$top_id);
        $where="1 and top_id=".$top_id;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and sub_name like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }
        $subcate=M('subcate');
        // 查询满足要求的总记录数
        $count      = $subcate->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $subcate->where($where)->order('sub_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);
        $this->assign('top_id',$top_id);
        $this->assign('indus_id',$indus_id);
        $this->display(); 
    }

    //添加二级行业评价分类
    public function addsub(){
       $top_id=I('get.top_id');
       $indus_id=session('indus_id','');
       if (IS_POST){
            $info=I("info");
            $info['top_id']=$top_id;
            $flag=M("Subcate")->add($info);
            if($flag){
                $this->success('添加二级名称成功！',U('indusvalue/indexsub',array('top_id'=>$top_id)),3);
            }else{
                $this->error('添加二级名称失败！',U('indusvalue/indexsub',array('top_id'=>$top_id)),3);
            }
        }else{
            $this->assign('indus_id',$indus_id);
            $this->assign('top_id',$top_id);
            $this->display();
        }
    }
    //修改二级行业评价分类
    public function editsub(){
        $indus_id=session('indus_id','');
        $top_id=I('get.top_id');
        $sub_id=I('get.sub_id');
        if (IS_POST){
            $info=I("info");
            $flag=M("Subcate")->where(array('sub_id'=>$sub_id))->save($info);
            if($flag){
                $this->success('编辑二级名称成功！',U('indusvalue/indexsub',array('top_id'=>$top_id)),3);
            }else{
                $this->error('编辑二级名称失败！',U('indusvalue/indexsub',array('top_id'=>$top_id)),3);
            }
        }else{
            $subcate=M('Subcate')->where(array('top_id'=>$top_id))->find();
            $this->assign('subcate',$subcate);
            $this->assign('indus_id',$indus_id);
            $this->assign('top_id',$top_id);
            $this->assign('sub_id',$sub_id);
            $this->display();
        }
    }

    //删除二级行业评价分类
    public function delsub(){
       $top_id=I('get.top_id');
       $sub_id=I("get.sub_id");
        M('Subcate')->where('sub_id='.$sub_id)->delete();
        $this->success('删除二级名称成功！',U('indusvalue/indexsub',array('top_id'=>$top_id)),3);
    }

//三级名称列表
    public function indexthree(){
        $sub_id=I('get.sub_id');
        $top_id=session('top_id','');
        $indus_id=session('indus_id','');
        $where="1 and sub_id=".$sub_id;
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword=trim($keyword)){$where.=" and three_name like '%$keyword%' ";$this->assign('keyword',$keyword);}
        }
        $threecate=M('Threecate');
        // 查询满足要求的总记录数
        $count      = $threecate->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $threecate->where($where)->order('sub_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);
        $this->assign('indus_id',$indus_id);
        $this->assign('top_id',$top_id);
        $this->assign('sub_id',$sub_id);
        $this->assign('three_id',$three_id);
        $this->display(); 
    }

    //添加三级行业评价分类
    public function addthree(){
       $sub_id=I('get.sub_id');
       $indus_id=session('indus_id','');
       $top_id=session('top_id','');
       if (IS_POST){
            $info=I("info");
            $info['sub_id']=$sub_id;
            $flag=M("Threecate")->add($info);
            if($flag){
                $this->success('添加三级名称成功！',U('indusvalue/indexthree',array('sub_id'=>$sub_id)),3);
            }else{
                $this->error('添加三级名称失败！',U('indusvalue/indexthree',array('sub_id'=>$sub_id)),3);
            }
        }else{
            $this->assign('indus_id',$indus_id);
            $this->assign('top_id',$top_id);
            $this->assign('sub_id',$sub_id);
            $this->display();
        }
    }
    //修改三级行业评价分类
    public function editthree(){
        $indus_id=session('indus_id','');
        $top_id=session('top_id','');
        $sub_id=I('get.sub_id');
        $three_id=I('get.three_id');
        if (IS_POST){
            $info=I("info");
            $flag=M("Threecate")->where(array('three_id'=>$three_id))->save($info);
            if($flag){
                $this->success('编辑三级名称成功！',U('indusvalue/indexthree',array('sub_id'=>$sub_id)),3);
            }else{
                $this->error('编辑三级名称失败！',U('indusvalue/indexthree',array('sub_id'=>$sub_id)),3);
            }
        }else{
            $threecate=M('Threecate')->where(array('three_id'=>$three_id))->find();
            $this->assign('threecate',$threecate);
            $this->assign('indus_id',$indus_id);
            $this->assign('top_id',$top_id);
            $this->assign('sub_id',$sub_id);
            $this->assign('three_id',$three_id);
            $this->display();
        }
    }

    //删除三级行业评价分类
    public function delthree(){
       $three_id=I('get.three_id');
       $sub_id=I("get.sub_id");
        M('Threecate')->where('three_id='.$three_id)->delete();
        $this->success('删除三级名称成功！',U('indusvalue/indexthree',array('sub_id'=>$sub_id)),3);
    }

    //选择管理员
    public function set_evaluate(){
        $where ="1 ";
        $admin = M('admin');
        // 查询满足要求的总记录数
        $count = $admin->where($where)->count();
        $Page = new \Think\Page($count, 20);
        $pageshow = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $admin->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('listinfo', $listinfo);
        $this->assign('pageshow', $pageshow);
        $this->display();
    }
 

}
